package com.mall.xiaomi.controller;

import com.mall.xiaomi.service.ShoppingCartService;
import com.mall.xiaomi.util.ResultMessage;
import com.mall.xiaomi.vo.CartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zhanghui
 * @ClassName ShoppingCartController
 * @Description TODO
 * @Date 2021/2/26 14:33
 * @Version 1.0
 **/
@RestController
@RequestMapping("/cart")
public class ShoppingCartController {
    @Autowired
    private ResultMessage resultMessage;
    @Autowired
    private ShoppingCartService cartService;

    /**
     * @Author zhanghui
     * @Description 根据用户id查询购物车信息接口
     * @Date 2021/2/26 14:42
     * @Param [userId]
     * @return com.mall.xiaomi.util.ResultMessage
     **/
    @GetMapping("/user/{userId}")
    public ResultMessage cart(@PathVariable String userId) {
        List<CartVo> carts = cartService.getCartByUserId(userId);
        resultMessage.success("001",carts);
        return resultMessage;
    }
    /**
     * @Author zhanghui
     * @Description 添加购物车
     * @Date 2021/2/26 15:21
     * @Param [productId, userId]
     * @return com.mall.xiaomi.util.ResultMessage
     **/
    @PostMapping("/product/user/{productId}/{userId}")
    public ResultMessage cart(@PathVariable String productId,@PathVariable String userId){
        CartVo cartVo = cartService.addCart(productId,userId);
        if(cartVo != null){
            resultMessage.success("001","添加购物车成功",cartVo);
        }else {
            resultMessage.success("002","该商品已在购物车上,数目+1");
        }
        return resultMessage;
    }

    /**
     * @Author zhanghui
     * @Description 修改商品数目,根据userId和productId的双向绑定
     * @Date 2021/2/26 15:27
     * @Param [cartId, userId, num]
     * @return com.mall.xiaomi.util.ResultMessage
     **/
    @PutMapping("/user/num/{cartId}/{userId}/{num}")
    public ResultMessage cart(@PathVariable String cartId,@PathVariable String userId,@PathVariable String num){
        cartService.updateCartNum(cartId,userId,num);
        resultMessage.success("001","更新成功");
        return resultMessage;
    }
    /**
     * @Author zhanghui
     * @Description 删除对应用户的购物车
     * @Date 2021/2/26 15:31
     * @Param [cartId, userId]
     * @return com.mall.xiaomi.util.ResultMessage
     **/
    @DeleteMapping("/user/{cartId}/{userId}")
    public ResultMessage deleteCart(@PathVariable String cartId,@PathVariable String userId){
        cartService.deleteCart(cartId,userId);
        resultMessage.success("001","删除成功");
        return resultMessage;
    }

}
